/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.maternity.classes.dao;

import br.com.maternity.classes.entity.Usuario;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Gattini
 */
public class UsuarioDAO extends GenericDAO<Usuario> {

    private static br.com.maternity.classes.entity.Usuario instance = new br.com.maternity.classes.entity.Usuario();

    public UsuarioDAO() {
        super(instance);
    }

    @Override
    public String tableName() {
        return "Usuario";
    }

    @Override
    public List<GenericDAOField> listFields() {
        List l = new ArrayList<GenericDAOField>();

        l.add(new GenericDAOField(
                "Login", "setLogin", "getLogin", String.class, true, GenericDAOFieldPrimaryKey.NOTHING));
        l.add(new GenericDAOField(
                "Senha", "setSenha", "getSenha", String.class));
        l.add(new GenericDAOField(
                "Tipo", "setTipo", "getTipo", Integer.class));

        return l;
    }

    public List<Usuario> selectAll() {
        return super.selectList("select * from " + tableName());
    }

    public Usuario login(String login, String senha, Integer tipoUsuario) {
        try {
            String query = "SELECT * FROM " + tableName() + " WHERE Login = ? AND Senha = ? AND Tipo = ?";
            PreparedStatement ps = super.getPreparedStatement(query);
            ps.setString(1, login);
            ps.setString(2, senha);
            ps.setInt(3, tipoUsuario);
            return super.select(ps);
        } catch (Exception ex) {
            return null;
        }
    }
    
        public Usuario getByLoginTipo(String login, Integer tipo) {
        try {
            String query = "SELECT * FROM " + tableName() + " WHERE Login = ? AND Tipo = ?";
            PreparedStatement ps = super.getPreparedStatement(query);
            ps.setString(1, login);
            ps.setInt(2, tipo);
            return super.select(ps);
        } catch (Exception ex) {
            return null;
        }
    }
}
